package classconverstation.cscl.ist.psu.edu.server;

import java.util.*;

import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;

/**
* Simple demonstration of using the javax.mail API.
*
* Run from the command line. Please edit the implementation
* to use correct email addresses and host name.
*/
public final class Emailer {

    public static void main(String... aArguments ){
        Emailer emailer = new Emailer();
        //the domains of these email addresses should be valid,
        //or the example will fail:
        emailer.sendEmail(
          "forum@cscl.ist.psu.edu", "hjiang@ist.psu.edu",
           "Testing 1-2-3", "blah blah blah"
        );
     }

    /**
    * Send a single email.
    */
    public void sendEmail(String aFromEmailAddr, String aToEmailAddr, String aSubject, String aBody){
        Session session = Session.getDefaultInstance( fMailServerConfig, null );
        MimeMessage message = new MimeMessage( session );
        try {
            message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(aToEmailAddr));
            message.setSubject( aSubject );
            message.setDataHandler(new DataHandler(aBody, "text/html"));
//            message.setText( aBody );
            Transport.send( message );
        }
        catch (MessagingException ex){
          System.err.println("Cannot send email. " + ex);
        }
    }
    
    public void sendEmail(String fromAddr, String[] toAddrs, String subject, String body) {
        Session session = Session.getDefaultInstance( fMailServerConfig, null );
        MimeMessage message = new MimeMessage( session );

        String mailer = "sendhtml";
        try {
            if (toAddrs.length < 1 || toAddrs == null) {
                return;
            }
            else {
                for (int i = 0; i < toAddrs.length; i++) {
                    message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toAddrs[i]));
                }
                message.setSubject( subject );
//                message.setContent(body, "text/html") ;
                
                message.setDataHandler(new DataHandler(
                        body, "text/html"));
            
            message.setHeader("X-Mailer", mailer);
            message.setSentDate(new Date());
                
                Transport.send( message );
            }
        }
        catch (MessagingException ex){
          System.err.println("Cannot send email. " + ex);
        }
    }

    /**
    * Allows the config to be refreshed at runtime, instead of
    * requiring a restart.
    */
    public static void refreshConfig() {
        fMailServerConfig.clear();
        fetchConfig();
    }

  // PRIVATE //

    private static Properties fMailServerConfig = new Properties();

    static {
        fetchConfig();
    }

    /**
    * Open a specific text file containing mail server
    * parameters, and populate a corresponding Properties object.
    */
    private static void fetchConfig() {
        fMailServerConfig.put("mail.smtp.host", "cscl.ist.psu.edu");
        fMailServerConfig.put("mail.from", "DoNotReply@cscl.ist.psu.edu");
    }
} 